

function initChart(canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr
  });
  canvas.setChart(chart);

  const option = {
    tooltip: {
      trigger: 'axis',
      formatter: '{b}: {c}kg'
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      top: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['1/4', '1/24', '1/31', '2/4', '2/14', '2/18', '3/1'],
      axisLine: {
        lineStyle: {
          color: '#999'
        }
      },
      axisLabel: {
        color: '#666'
      }
    },
    yAxis: {
      type: 'value',
      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      },
      splitLine: {
        lineStyle: {
          color: '#eee',
          type: 'dashed'
        }
      },
      axisLabel: {
        color: '#666'
      }
    },
    series: [{
      data: [49.0, 49.5, 50.0, 52.0, 56.0, 54.0, 52.0],
      type: 'line',
      smooth: true,
      symbol: 'circle',
      symbolSize: 8,
      itemStyle: {
        color: '#1296db'
      },
      lineStyle: {
        color: '#1296db',
        width: 3
      },
      areaStyle: {
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [{
            offset: 0,
            color: 'rgba(18,150,219,0.3)'
          }, {
            offset: 1,
            color: 'rgba(18,150,219,0)'
          }]
        }
      }
    }]
  };

  chart.setOption(option);
  return chart;
}
import request from '../../../../utils/request'
Page({
  data: {
    ec: {
      onInit: initChart
    },
    currentDate: '2021年3月1日 14:00',
    weight: '52.0',
    bmi: '20.7',
    bmiStatus: '标准',
    showModal: false,
    inputWeight: '',
    weightRecords: [
      {
        id: 1,
        date: '01/24 2020',
        weight: '52.0',
        status: '正常'
      }
      // 可以添加更多记录
    ]
  },

  onLoad: function() {
    this.getUserWeight()
    .then(userweight=>{
        this.setData({
            userweight:userweight
        })
    })
  },
//获取用户体重
getUserWeight(){
    return new Promise((resolve,reject)=>{
        let userId = wx.getStorageSync('userid')
        let weightid = 1  //数据库中体重id=1 
        let url = `healthlog/healthlog/selectByUseridAndTypeid?userid=${userId}&typeid=${weightid}`
        request({
            url:url,
            method:'GET',
            success(res){
                // console.log("体重列表：",res.data);
                let userweight = res.data
                resolve(userweight)
            },fail(error){
                reject(error)
            }
        })
    })
  },
  goBack: function() {
    wx.navigateBack({
      delta: 1
    });
  },

  showWeightModal: function() {
    this.setData({
      showModal: true,
      inputWeight: ''
    });
  },

  hideWeightModal: function() {
    this.setData({
      showModal: false
    });
  },

  onWeightInput: function(e) {
    this.setData({
      inputWeight: e.detail.value
    });
  },

  saveWeight: function() {
    if (!this.data.inputWeight) {
      wx.showToast({
        title: '请输入体重',
        icon: 'none'
      });
      return;
    }

    // 这里添加保存体重的逻辑
    const weight = parseFloat(this.data.inputWeight);
    // 获取当前时间并格式化为 YYYY-MM-DD HH:mm:ss 的形式
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const day = String(now.getDate()).padStart(2, '0');
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');
    const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

    // 从本地存储中获取当前用户的 ID
    const userId = wx.getStorageSync('userid');

    // 构建要插入数据库的体重记录对象
    const record = {
        number: weight, // 体重数值
        typeid: 1, // 体重类型 ID，固定值为 3
        time: formattedTime, // 当前时间
        source: 1, // source 字段，固定值为 1
        userId: userId // 当前用户的 ID
    };

    // 定义数据库插入接口的 URL
    let url = `healthlog/healthlog`;
    // 发送 POST 请求将数据插入数据库
    request({
        url: url,
        method: 'POST',
        data: record,
        success: (res) => {
            // 插入成功时的处理逻辑
            console.log("体重数据插入成功", res.data);
            wx.showToast({
                title: '保存成功',
                icon: 'success'
            });
            // 插入成功后重新获取体重列表
            this.getUserWeight()
                .then(userweight => {
                    this.setData({
                        userweight: userweight
                    });
                    // 获取成功后关闭弹窗并清空输入框
                    this.setData({
                        showModal: false,
                        inputSystolic: ''
                    });
                })
                .catch(error => {
                    console.error("获取体重列表失败", error);
                    wx.showToast({
                        title: '获取体重数据失败',
                        icon: 'none'
                    });
                });
        },
        fail: (error) => {
            // 插入失败时的处理逻辑
            console.error("体重数据插入失败", error);
            wx.showToast({
                title: '保存失败',
                icon: 'none'
            });
        }

    });
  },

  editWeight: function(e) {
    const id = e.currentTarget.dataset.id;
    const record = this.data.weightRecords.find(item => item.id === id);
    if (record) {
      this.setData({
        showModal: true,
        inputWeight: record.weight
      });
    }
  },

  deleteWeight: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除这条记录吗？',
      success: res => {
        if (res.confirm) {
          const weightRecords = this.data.weightRecords.filter(item => item.id !== id);
          this.setData({
            weightRecords
          });
          wx.showToast({
            title: '删除成功',
            icon: 'success'
          });
        }
      }
    });
  }
}); 